package com.meizhao.admin.utils;

import cn.hutool.crypto.SecureUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.time.Clock;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Slf4j
public final class OtherUtils {

	public static String encryptSha1(String str){

		if(str==null||str.length()==0){
			return null;
		}
		char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9',
				'a','b','c','d','e','f'};
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
			mdTemp.update(str.getBytes(Charset.forName("UTF-8")));

			byte[] md = mdTemp.digest();
			int j = md.length;
			char buf[] = new char[j*2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
				buf[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(buf);
		} catch (Exception e) {
			return null;
		}
	}


	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
	}


	public static String[] generateMsgCode(String encodeRules,String mobile) {
		String code = String.valueOf(ThreadLocalRandom.current().nextInt(9999-1000+1)+1000);
		//电话_code_time
		String str = SymmetricEncoder.AESEncode(encodeRules, mobile+"_"+code+"_"+ Clock.systemUTC().millis());
		return new String[]{code, str};
	}

	public static String generateEncryptionString(String encodeRules,String rawStr) {

		return SymmetricEncoder.AESEncode(encodeRules, rawStr);
	}

	public static Field[] addAll(Field[] target, Field[] source) {

		if (target != null) {
			List<Field> fieldTarget = Stream.of(target).collect(Collectors.toList());
			if (source != null) {
				List<Field> fieldsSource = Stream.of(source).collect(Collectors.toList());
				fieldTarget.addAll(fieldsSource);
			}
			target = fieldTarget.toArray(new Field[fieldTarget.size()]);
			return target;
		}
		return target;
	}

	public static String getMD5(String str){

		return SecureUtil.md5(str);
	}


	public static String getFileType(List<String> fileTypes, byte[] data) {
		//提取前六位作为魔数
		String magicNumberHex = getHex(data);
		for(String str : fileTypes){
			String[] strs = str.split("_");
			if(strs[1].equals(magicNumberHex)){
				return strs[0];
			}
		}
		return null;
	}

	/**
	 * 获取16进制表示的魔数
	 *
	 * @param data              字节数组形式的文件数据
	 * @return
	 */
	private static String getHex(byte[] data) {
		//提取文件的魔数
		StringBuilder magicNumber = new StringBuilder();
		//一个字节对应魔数的两位
		for (int i = 0; i < 3; i++) {
			magicNumber.append(Integer.toHexString(data[i] >> 4 & 0xF));
			magicNumber.append(Integer.toHexString(data[i] & 0xF));
		}
		return magicNumber.toString().toUpperCase();
	}

	public static String getPwd(String pwd, String salt){

		return OtherUtils.encryptSha1(OtherUtils.getMD5(pwd)+salt);
	}

	public static String hmacSHA256(String src, String key) {
		try {
			SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("utf-8"), "HmacSHA256");
			Mac mac = Mac.getInstance("HmacSHA256");
			mac.init(signingKey);
			byte[] rawHmac = mac.doFinal(src.getBytes("utf-8"));
			return Hex.encodeHexString(rawHmac).toLowerCase();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String toJson(Object object) {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.setPrettyPrinting();
		Gson gson = gsonBuilder.create();
		return gson.toJson(object);
	}
	
	
}
